﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :NewsClass.aspx.cs
//        description :新闻类别列表页面
//
//        created by 邹兴武 at  2010-12-29
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TreeViewList.UC;
using System.Web.Services;

namespace huaguwang.hgmanage.News
{
    public partial class NewsClass : System.Web.UI.Page
    {
        private readonly D_T_NewsCate_Exp _dal = new D_T_NewsCate_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserToolBar1.MainInit(
              UserConst.Exp_GetUser,
              UserConst.Exp_GetButtonList,
             SetBut,
             UserConst.Exp_ToolBarErr
           );

            if (!IsPostBack)
            {
                BindTreeList();
            }
        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.Attributes.Add("onclick", "return SelectEvent();");
                    break;
                case 2:
                    but.Attributes.Add("onclick", "return del();");
                    but.Attributes.Add("onmousedown", "return DelEvent();");
                    break;
                case 3:
                    but.Attributes.Add("onclick", "return moveup(\"news\");");
                    break;
                case 4:
                    but.Attributes.Add("onclick", "return movedown(\"news\");");
                    break;
                default:
                    break;
            }
        }

        [WebMethod]
        public static bool MoveUp(int id)
        {
            D_T_NewsCate_Exp cc = new D_T_NewsCate_Exp();
            return cc.MoveClass(id, true);
        }

        [WebMethod]
        public static bool MoveDown(int id)
        {
            D_T_NewsCate_Exp cc = new D_T_NewsCate_Exp();
            return cc.MoveClass(id, false);
        }

        [WebMethod]
        public static int IsEffective(int classid)
        {
            D_T_NewsCate_Exp _dal = new D_T_NewsCate_Exp();
            var bln = _dal.HasChildNodes(classid);
            if (bln)
            {
                return 1;
            }
            else
            {
                bln = _dal.IsEffective(classid);
                if (bln)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 绑定信息列表
        /// </summary>
        protected string BindTreeList()
        {
            var list = _dal.GetCateList();
            var ucList = from li in list
                         select new CatagroyViewList.NodeItem()
                         {
                             ID = li.id,
                             Depth = li.Depth,
                             Title = li.CateName,
                             ParentID = li.ParentId
                         };

            return CatagroyViewList.BindTreeList(ucList.ToList());
        }

        /// <summary>
        /// 新增按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            Response.Redirect("newsclass_e.aspx");
        }

        /// <summary>
        /// 修改按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
                {
                    var id = Request.Params["HiddenID"].Exp_IntTryParse();
                    var vResult = _dal.GetModel(id);
                    if (vResult == null)
                    {
                        this.Exp_PageAlter("没有可维护的资料");
                        return;
                    }
                    var key = this.WebUserToolBar1.EditSKey;

                    Response.Redirect("newsclass_e.aspx?id=" + id + "&" + key);
                });
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
                {
                    return;
                    var id = Request.Params["HiddenID"].Exp_IntTryParse();
                    if (_dal.UpdateFlag(id) > 0)
                    {
                        _dal.UpdateExp(id);
                        this.Exp_PageWriteLog(Log_Type.delete, "新闻类别", id);
                        this.Exp_PageAlterHref("删除成功", "newsclass.aspx");
                    }
                    else
                    {
                        this.Exp_PageAlter("删除操作失败!");
                    }
                });
        }

    }
}
